/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * Author: Junlin Liu
 * Date: 2025-06-19
 */
package numaaffinity

import (
	"testing"

	"openfuyao.com/colocation-management/pkg/colocation-overquota-agent/nriserver/core"
	"openfuyao.com/colocation-management/pkg/utils"
)

// 只测单例
func TestGetNUMAAffinityRunner(t *testing.T) {
	numaAffinityRunner = nil
	r1 := GetNUMAAffinityRunner()
	r2 := GetNUMAAffinityRunner()
	if r1 != r2 {
		t.Error("GetNUMAAffinityRunner should return the same instance")
	}
}

// 只测 applyNUMAAffinityForContainer 的 happy path 和部分异常
func TestApplyNUMAAffinityForContainer(t *testing.T) {
	topology := &core.NumaTopology{
		Nodes: map[int]*core.NumaNode{
			0: {ID: 0, CPUList: []int{0, 1, 2}},
		},
	}
	container := &core.ContainerInfo{
		ContainerRequest: core.ContainerRequest{
			ContainerMeta:   core.ContainerMetadata{ID: "c1"},
			CPUMilliRequest: 2000, // 2核
		},
		ContainerResponese: core.ContainerResponse{
			Resources: utils.LinuxContainerResources{},
		},
	}
	err := applyNUMAAffinityForContainer(topology, container)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if container.ContainerResponese.Resources.CPUSet == nil || *container.ContainerResponese.Resources.CPUSet == "" {
		t.Error("CPUSet should be set")
	}

	// 异常：topology为nil
	err = applyNUMAAffinityForContainer(nil, container)
	if err == nil {
		t.Error("expected error when topology is nil")
	}

	// 异常：container为nil
	err = applyNUMAAffinityForContainer(topology, nil)
	if err == nil {
		t.Error("expected error when container is nil")
	}
}
